﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace System.Utility
{
    #region Enums
    public enum LogLevel { DEBUG, INFO, WARN, FATAL };

    public enum ExceptionLevel { WARN, ERROR, FATAL };

    public enum LogMessageType
    {
        ApplicationMessage,
        WebMessage
    }
    #endregion

    public static class Logger
    {
        #region Private Constants
        private const string AppLoggerKey = "ApplicationLogger";
        private const string WebLoggerKey = "WebLogger";

        private const string StringFormat = "{0}\n{1}\n{2}";
        #endregion

        private static Dictionary<LogMessageType, ILog> loggerSelect;

        static Logger()
        {
            //Init();
        }

        public static void Init()
        {
            try
            {
                var appLogger = log4net.LogManager.GetLogger(AppLoggerKey);
                var webLogger = log4net.LogManager.GetLogger(WebLoggerKey);

                log4net.Config.XmlConfigurator.Configure();

                loggerSelect = new Dictionary<LogMessageType, ILog>();
                loggerSelect.Add(LogMessageType.ApplicationMessage, appLogger);
                loggerSelect.Add(LogMessageType.WebMessage, webLogger);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw; //kill application if logging init fails
            }
        }

        public static void LogException(string msg, Exception ex, ExceptionLevel errorLevel, LogMessageType msgType)
        {
            //switch (errorLevel)
            //{
            //    case ExceptionLevel.WARN:
            //        loggerSelect[msgType].Warn(string.Format(StringFormat, msg, ex.Message, ex.StackTrace));
            //        break;
            //    case ExceptionLevel.ERROR:
            //        loggerSelect[msgType].Error(string.Format(StringFormat, msg, ex.Message, ex.StackTrace));
            //        break;
            //    case ExceptionLevel.FATAL:
            //        loggerSelect[msgType].Fatal(string.Format(StringFormat, msg, ex.Message, ex.StackTrace));
            //        break;
            //}
        }

        public static void LogMessage(string msg, LogLevel messageLevel, LogMessageType msgType)
        {
            //switch (messageLevel)
            //{
            //    case LogLevel.DEBUG:
            //        loggerSelect[msgType].Debug(msg);
            //        break;
            //    case LogLevel.INFO:
            //        loggerSelect[msgType].Info(msg);
            //        break;
            //    case LogLevel.WARN:
            //        loggerSelect[msgType].Warn(msg);
            //        break;
            //    case LogLevel.FATAL:
            //        loggerSelect[msgType].Fatal(msg);
            //        break;
            //}
        }
    }

}
